package Model;


import java.util.Random;

public class Filter {
    //custom use :
    //int[] newPixels = Filter.WhiteNoise(0,0,width,height, width, oldPixels, p, d);
    //myImage.setRGB(..., newPixel, ...);
    public static int[] WhiteNoise(int startX, int startY, int width, int height, int dataWidth, int[] data, double p, int d){
        Random rn = new Random();
        int[] result = new int[width*height];
        double randomDoubleVal;
        int randomIntValR, randomIntValG, randomIntValB;
        for (int i = 0; i != height; ++i){
            for (int j = 0; j != width; ++j){
                int pos =  i * width + j;
                result[pos] = data[(startY + i) * dataWidth + startX + j];
                randomDoubleVal = rn.nextDouble();
                if (randomDoubleVal <= p) {
                    int temp = result[pos];
                    randomIntValR = (rn.nextInt() %  (2 * d + 1)) - d;
                    randomIntValG = (rn.nextInt() %  (2 * d + 1)) - d;
                    randomIntValB = (rn.nextInt() %  (2 * d + 1)) - d;

                    int r = (temp & 0xff) + randomIntValR;
                    if (r < 0)
                        r = 0;
                    if (r > 0xff)
                        r = 0xff;

                    int g = ((temp & 0xff00) >> 8) + randomIntValG;
                    if (g < 0)
                        g = 0;
                    if (g > 0xff)
                        g = 0xff;

                    int b = ((temp & 0xff0000) >> 16) + randomIntValB;
                    if (b < 0)
                        b = 0;
                    if (b > 0xff)
                        b = 0xff;

                    result[pos] = r + (g << 8)  + (b << 16);
                }
            }
        }
        return result;
    }

    public static int[] saltPepper(int startX, int startY, int width, int height, int dataWidth, int[] data, double p){
        Random rn = new Random();
        int[] result = new int[width*height];
        double randomDoubleVal;
        int randomInt;
        double realP = p;
        for (int i = 0; i != height; ++i){
            for (int j = 0; j != width; ++j){
                int pos =  i * width + j;
                result[pos] = data[(startY + i) * dataWidth + startX + j];
                randomDoubleVal = rn.nextDouble();
                if (randomDoubleVal <= realP) {
                    randomInt = rn.nextInt() %  2;
                    result[pos] = randomInt == 1 ? 0xffffff : 0x000000;
                }
            }
        }
        return result;
    }

    public static int[] Dust(int startX, int startY, int width, int height, int dataWidth, int[] data, double p, int min){
        Random rn = new Random();
        int[] result = new int[width*height];
        double randomDoubleVal;
        int randomIntValR, randomIntValG, randomIntValB;
        for (int i = 0; i != height; ++i){
            for (int j = 0; j != width; ++j){
                int pos =  i * width + j;
                result[pos] = data[(startY + i) * dataWidth + startX + j];
                randomDoubleVal = rn.nextDouble();
                if (randomDoubleVal <= p) {
                    int temp = result[pos];
                    randomIntValR = min + rn.nextInt() %  (255 - min + 1);
                    randomIntValG = min + rn.nextInt() %  (255 - min + 1);
                    randomIntValB = min + rn.nextInt() %  (255 - min + 1);

                    int r = (temp & 0xff) + randomIntValR;
                    if (r > 0xff)
                        r = 0xff;

                    int g = ((temp & 0xff00) >> 8) + randomIntValG;
                    if (g > 0xff)
                        g = 0xff;

                    int b = ((temp & 0xff0000) >> 16) + randomIntValB;
                    if (b > 0xff)
                        b = 0xff;

                    result[pos] = r + (g << 8)  + (b << 16);
                }
            }
        }
        return result;
    }
}
